package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class MQConnectionPoolView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();
		setSpacing(true);
        setMargin(true);
        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
        final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        //MinConnections
        String minConnectionValue=wasConfigProperty.getString("qcfMinConnections", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".MinConnections"));
        final TextField minConnectionText = new TextField("MinConnections");
        minConnectionText.setValue(minConnectionValue);
        minConnectionText.setWidth("100%");
        minConnectionText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfMinConnections", value);
   			}
           	
           });
        minConnectionText.setImmediate(true);
        form.addComponent(minConnectionText);
        
       //MaxConnections
        String maxConnectionValue=wasConfigProperty.getString("qcfMaxConnections", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".MaxConnections"));
        final TextField maxConnectionText = new TextField("MaxConnections");
        maxConnectionText.setValue(maxConnectionValue);
        maxConnectionText.setWidth("100%");
        maxConnectionText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfMaxConnections", value);
   			}
           	
           });
        maxConnectionText.setImmediate(true);
        form.addComponent(maxConnectionText);
        
       //ConnectionTimeout
        String connectionTimeoutValue=wasConfigProperty.getString("qcfConnectionTimeout", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".ConnectionTimeout"));
        final TextField connectionTimeoutText = new TextField("ConnectionTimeout");
        connectionTimeoutText.setValue(maxConnectionValue);
        connectionTimeoutText.setWidth("100%");
        connectionTimeoutText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfConnectionTimeout", value);
   			}
           	
           });
        connectionTimeoutText.setImmediate(true);
        form.addComponent(connectionTimeoutText);
        
       //ReapTime 
        String reapTimeValue=wasConfigProperty.getString("qcfReapTime", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".ReapTime"));
        final TextField reapTimeText = new TextField("ReapTime");
        reapTimeText.setValue(reapTimeValue);
        reapTimeText.setWidth("100%");
        reapTimeText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfReapTime", value);
   			}
           	
           });
        reapTimeText.setImmediate(true);
        form.addComponent(reapTimeText);
        
        //UnusedTimeout 
        String unusedTimeoutValue=wasConfigProperty.getString("qcfUnsedTimeout", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".UnusedTimeout"));
        final TextField unusedTimeoutText = new TextField("UnusedTimeout");
        unusedTimeoutText.setValue(reapTimeValue);
        unusedTimeoutText.setWidth("100%");
        unusedTimeoutText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfUnsedTimeout", value);
   			}
           	
           });
        unusedTimeoutText.setImmediate(true);
        form.addComponent(unusedTimeoutText);
        
        
      //AgedTimeout 
        String agedTimeoutValue=wasConfigProperty.getString("qcfAgedTimeout", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".AgedTimeout"));
        final TextField agedTimeoutText = new TextField("AgedTimeout");
        agedTimeoutText.setValue(agedTimeoutValue);
        agedTimeoutText.setWidth("100%");
        agedTimeoutText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfAgedTimeout", value);
   			}
           	
           });
        agedTimeoutText.setImmediate(true);
        form.addComponent(agedTimeoutText);
        
        //AgedTimeout 
        String purgePolicyValue=wasConfigProperty.getString("qcfPurgePolicy", WasConstant.getPropertyValue(WasConstant.WAS_XML_QCFCONNECTIONPOOL_NAME+".PurgePolicy"));
        final TextField purgePolicyText = new TextField("PurgePolicy");
        purgePolicyText.setValue(purgePolicyValue);
        purgePolicyText.setWidth("100%");
        purgePolicyText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qcfPurgePolicy", value);
   			}
           	
           });
        purgePolicyText.setImmediate(true);
        form.addComponent(purgePolicyText);
		
	}

}
